﻿using Ozone.Help;
using Ozone.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Ozone.DAL
{
    public class Sys_DictDAL
    {
        private static List<sys_dict> _allsys_dict;

        /// <summary>
        /// 添加字典信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool AddDictInfo(sys_dict Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Insertable<sys_dict>(Model).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("添加字典信息异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("添加字典信息异常");
            return IsSuccess;
        }

        /// <summary>
        /// 删除字典信息
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public static bool DeleteDictInfo(string ID)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(ID))
                {
                    IsSuccess = db.Deleteable<sys_dict>().Where(p => p.FID == ID).ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("删除字典异常", ex);
                IsSuccess = false;
            }
            return IsSuccess;
        }

        /// <summary>
        /// 修改字典信息
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public static bool UpdateDictInfo(sys_dict Model)
        {
            bool IsSuccess = false;
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (Model != null)
                {
                    IsSuccess = db.Updateable<sys_dict>()
                    .SetColumns(p => p.ItemCode == Model.ItemCode)
                    .SetColumns(p => p.ItemName == Model.ItemName)
                    .SetColumns(p => p.IsEnabled == Model.IsEnabled)
                    .SetColumns(p => p.ExtendData == Model.ExtendData)
                    .SetColumns(p => p.ModifyTime == Model.ModifyTime)
                    .Where(p => p.FID == Model.FID)
                    .ExecuteCommand() > 0;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("更新字典信息异常", ex);
                IsSuccess = false;
            }
            LogHelper.Debug("更新字典信息异常");
            return IsSuccess;
        }

        /// <summary>
        /// 查询字典信息
        /// </summary>
        /// <param name="Starttime"></param>
        /// <param name="EndTime"></param>
        /// <param name="FID"></param>
        /// <returns></returns>
        public static List<sys_dict> GetDictInfoList(string ID)
        {
            try
            {
                SqlSugarScope db = SqlSugarOper.Instance.GetDbClient();
                if (!string.IsNullOrEmpty(ID))
                {
                    _allsys_dict = db.Queryable<sys_dict>()
                    .Where(p => p.FID == ID && p.IsEnabled == 1)
                    .OrderBy(p => p.CreateTime, OrderByType.Desc)
                    .ToList();
                }
                else
                {
                    //查询所有字典信息
                    _allsys_dict = db.Queryable<sys_dict>()
                    .Where(p => p.IsEnabled == 1)
                   .OrderBy(p => p.CreateTime, OrderByType.Desc)
                   .ToList();
                }
            }
            catch (Exception ex)
            {
                LogHelper.Error("查询字典信息发生异常", ex);
                _allsys_dict = null;
            }
            return _allsys_dict;
        }
    }
}
